package cn.edu.hnu.gpg.service;

import cn.edu.hnu.gpg.dao.AnswerMapper;
import cn.edu.hnu.gpg.entity.Answer;
import cn.edu.hnu.gpg.entity.AnswerExample;
import cn.edu.hnu.gpg.entity.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AnswerServiceImpl implements  AnswerService{
    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    public AnswerServiceImpl(AnswerMapper answerMapper){
        this.answerMapper = answerMapper;
    }

    @Override
    public List<Answer> getAllAnswerToQuestion(int question_id) {
        if (question_id <1 )
            return null;

        AnswerExample answerExample = new AnswerExample();
        answerExample.or().andQuestionIdEqualTo(question_id);
        List<Answer> answers = answerMapper.selectByExampleWithBLOBs(answerExample);
//        Answer answer = new Answer();
//        if (answers == null){
//            answer.setQuestionId(question_id);
//            answer.setAnswerTime(new Date());
//            answer.setContent("尚未有有效回答，请静待做出回答");
//        }
//        List<Answer> answerList = new ArrayList<>();
//        answerList.add(answer);
//        return answers.size()>0 ? answers : answerList;
        return answers.size()>0 ? answers : null;
    }

    @Override
    public List<Answer> getAllAnswerToQuestion(Question question) {
        if (question == null)
            return null;

        return getAllAnswerToQuestion(question.getQuestionId());
    }

    @Override
    public String getFatherContent(Answer answer) {
        if (answer == null)
            return null;

        return getFatherContent(answer.getToAnswerId());
    }

    @Override
    public String getFatherContent(int to_answer_id) {
        if (to_answer_id == 0)
            return null;

        AnswerExample answerExample = new AnswerExample();
        answerExample.or().andToAnswerIdEqualTo(to_answer_id);
        List<Answer> answers = answerMapper.selectByExampleWithBLOBs(answerExample);
        return answers.size()>0 ? answers.get(0).getContent() : null;
    }

    @Override
    public int createAnswer(int author, int question_id, int to_answer_id, String content) {
        if (author < 1)
            return 0;
        if (question_id < 1)
            return 0;

        Answer answer = new Answer();
        answer.setAuthor(author);
        answer.setContent(content);
        answer.setQuestionId(question_id);
        answer.setToAnswerId(to_answer_id);
        int id = answerMapper.insertSelective(answer);
        return id;
    }

    @Override
    public int createAnswer(int author, int question_id, String content) {
        if (author < 1)
            return 0;
        if (question_id < 1)
            return 0;

        Answer answer = new Answer();
        answer.setAuthor(author);
        answer.setContent(content);
        answer.setQuestionId(question_id);
        int id = answerMapper.insertSelective(answer);
        return id;
    }

    @Override
    public int updateByQuestionId(int question_id, String content) {
        AnswerExample answerExample = new AnswerExample();
        answerExample.or().andQuestionIdEqualTo(question_id);
        List<Answer> answers = answerMapper.selectByExampleWithBLOBs(answerExample);
        if (answers.size()<=0)return 0;
        Answer answer = answers.get(0);
        answer.setContent(content);
        return answerMapper.updateByPrimaryKeyWithBLOBs(answer);
    }

    @Override
    public boolean deleteAnswer(int answer_id) {
        if (answer_id < 1)
            return false;

        return answerMapper.deleteByPrimaryKey(answer_id)>0;
    }

}
